package com.xsq.audio.player;

import android.content.Context;
import android.support.v4.media.MediaMetadataCompat;

import com.xsq.audio.AudioController;
import com.xsq.audio.AudioDataSource;
import com.xsq.audio.AudioItemData;
import com.xsq.audio.IAudioController;
import com.xsq.audio.IAudioControllerListener;
import com.xsq.audio.IAudioSubscriptionCallback;
import com.xsq.audio.player.bean.AudioPositionBean;
import com.xsq.audio.player.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import io.reactivex.rxjava3.core.Observable;

/**
 * 调用控制器单例
 */

public class PlayManager {
    public static IAudioController audioController = null;
    private static Context mContext;
    private static volatile boolean hasInit = false;

    /**
     * 使用音乐播放其时，一定要先调用该方法初始化，可以放在Application中初始化
     */
    public synchronized static void startAudioService(Context context, List<AudioItemData> audioItemList) {
        if (!hasInit) {
            mContext = context.getApplicationContext();
            audioController = new AudioController.Builder(mContext)
                    .audioItemList(audioItemList)
                    .build();
            audioController.startService();
            hasInit = true;
        }
    }

    public static Context getContext() {
        return mContext;
    }

    /**
     * 订阅指定专辑，目前默认是一个根专辑为/，为后期需要扩展有多个专辑
     */
    public static void subscribe(IAudioSubscriptionCallback callback) {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.subscribe(AudioDataSource.BROWSER_ROOT_ID, callback);
    }

    /**
     * 一对一订阅和取消订阅
     */
    public static void unsubscribe(IAudioSubscriptionCallback callback) {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.unsubscribe(AudioDataSource.BROWSER_ROOT_ID, callback);
    }

    /**
     * 设置需要播放的音频列表   注意：需要连接服务才能调用
     * @param audioItemList 需要播放音频列表集合
     */
    public static void setAudioItemList(List<AudioItemData> audioItemList) {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.setAudioItemList(AudioDataSource.BROWSER_ROOT_ID, audioItemList);
    }

    /**
     * 播放指定音频id选项
     */
    public static void playAudioId(String audioId) {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.playAudioId(audioId);
    }

    /**
     * 播放指定媒体选项
     * @param data 音频列表中指定选项
     */
    public static void playAudioItem(AudioItemData data) {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.playAudioItem(data);
    }

    /**
     * 下一首
     */
    public static void nextPlay() {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.nextAudio();
    }

    /**
     * 上一首
     */
    public static void previous() {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.previousAudio();
    }

    /**
     * 开始播放当前正在执行音频
     */
    public static void play() {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.play();
    }

    /**
     * 暂停正在执行音频
     */
    public static void pause() {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.pause();
    }

    /**
     * 停止正在播放的音频
     */
    public static void stop() {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.stop();
    }

    /**
     * 控制当前播放的因为指定时间位置
     */
    public static void seekTo(int ms) {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.seekTo(ms);
    }

    /**
     * 设置正在播放模式
     */
    public static void setRepeatMode(int repeatMode) {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.setRepeatMode(repeatMode);
    }

    /**
     * 设置随机播放模式
     */
    public static void setShuffleMode(int shuffleMode) {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.setShuffleMode(shuffleMode);
    }

    /**
     * 获取正在播放模式
     */
    public static int getRepeatMode() {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return 0;
        }
        return audioController.getRepeatMode();
    }

    /**
     * 获取当前播放数据
     */
    public static MediaMetadataCompat getCurPlayingData() {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return null;
        }
        return audioController.getCurPlayingData();
    }

    public static List<AudioItemData> getAudioItemList() {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return new ArrayList<>();
        }
        return audioController.getCurAudioItemList();
    }

    /**
     * 监听音频播放进度信息
     */
    public static Observable<AudioPositionBean> onPlayingPosBus() {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return Observable.just(new AudioPositionBean());
        }
        return audioController.onPlayingPosBus();
    }

    /**
     * 添加控制器与服务器通信回调监听
     */
    public static void addIAudioControllerListener(IAudioControllerListener listener) {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.addIAudioControllerListener(listener);
    }

    /**
     * 删除指定控制器回调监听
     */
    public static void removeIAudioControllerListener(IAudioControllerListener listener) {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return;
        }
        audioController.removeIAudioControllerListener(listener);
    }

    /**
     * 音频列表正在播放的选项
     */
    public static int getCurrentItemPosition() {
        try {
            Objects.requireNonNull(audioController, "audioController is null");
        } catch (NullPointerException e) {
            LogUtil.e(e.getMessage());
            return 0;
        }
        return audioController.getCurrentItemPosition();
    }

    /**
     * 是否正在播放
     */
    public static boolean isPlaying() {
        if (audioController == null) return false;
        return audioController.isPlaying();
    }

    /**
     * 关闭音乐服务,释放资源
     */
    public static void close() {
        if (hasInit) {
            try {
                Objects.requireNonNull(audioController, "audioController is null");
            } catch (NullPointerException e) {
                LogUtil.e(e.getMessage());
                return;
            }
            audioController.close();
            audioController = null;
            hasInit = false;
        }
    }

}
